package org.misalen.web.service.file.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.misalen.web.service.file.InfoType;
import org.misalen.web.service.file.OssFile;
import org.springframework.stereotype.Service;

import com.aliyun.oss.OSSClient;

@Service
public class OssFileImpl implements OssFile {
	private OSSClient client;
	static Properties p = new Properties();
	static {
		InputStream in = OssFileImpl.class.getClassLoader().getResourceAsStream("pro.properties");
		try {
			p.load(in);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public OssFileImpl() {
		client = new OSSClient("http://" + p.getProperty("file.oss.endpoint") + "/",
				p.getProperty("file.oss.accessKeyId"), p.getProperty("file.oss.accessKeySecret"));
	}

	@Override
	public String uploadFile(InputStream input, String key, InfoType infoType) {
		try {
			String bucketName = p.getProperty(infoType.bucketName);
			client.putObject(bucketName, infoType.folder + key, input);
			return "http://" + bucketName + "." + p.getProperty("file.oss.endpoint") + "/" + infoType.folder + key;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

}
